<?php 
class ShopListController extends Controller{
	private $model;
	public function __construct(){
		parent::__construct();
		$this->model=K('Shoplist');
	}
	public function index(){
//		通过gid循环出属性列表
       $gid=Q('get.gid',0,'intval');
	  
	   if(IS_POST){
//      提交添加或品列表
//      获得提交的属性
	   	$arr=$_POST['gsid'];
	
		foreach($arr as $v){
			if($v==0){
				$this->error("请选择属性");
			}
			
		}
		
		$strgsid=implode(',',$arr);
		$_POST['sumattr']=$strgsid;
		$_POST['goods_gid']=$gid;
		if(!$this->model->addData()){
			$this->error($this->model->error);
		}
		$this->success("货品添加成功");
	   }
//	   循环出货品列表的数据
      $shopData=$this->model->where("goods_gid={$gid}")->all();
	  foreach($shopData as $k=> $v){
	  	$attrData=explode(',',$v['sumattr']);
	    $shopData[$k]['sumattr']=$attrData;
	  }
//	p($shopData);die;
	

	  $this->assign('shopData',$shopData);
	   
	   
//	   通过gid在商品属性表中找到属性，关联typeproperty表
       $attrData=M()->join('__goodsattr__ gs JOIN __typeproperty__ tp ON gs.typeproperty_id=tp.id')->
       where("goods_gid={$gid}")->field("*,gs.id AS gsid")->all();
//	   p($attrData);
//	   获得typroperty_id集合
	
	  

	  $attrgroup=array();
	  foreach($attrData as $k=>$v){
	  	$attrgroup[$v['typeproperty_id']][]=$v;
	  }



	   $this->assign("attrgroup",$attrgroup);
	 
	   
	   
	

        
		$this->display();
	}
//修改货号
     public function edit(){
//   	获得商品id
     	$gid=$_GET['gid'];
//		获得商品属性名称
        

//   	获得货号id
        $id=$_GET['id'];
		

		if(IS_POST){
//			将属性字符串转化为数组
            $gsid=$_POST['gsid'];
            $gsid=implode(',',$gsid);
			$gid=$_POST['gid'];
			$id=$_POST['id'];
			$_POST['sumattr']=$gsid;
			if(!$this->model->updateData($id)){
				$this->error($this->model->error);
				
			}
			$this->success("货号编辑成功");
			
			

		}
//		获得所有商品属性数据
        $shopAttr=M("goodsattr")->where("goods_gid='{$gid}'")->order("typeproperty_id ASC")->all();
		
//		循环$shopAttr，将同一属性归在一起
        $attrData=array();
		
        foreach($shopAttr as $v){
        	$typeproperty_id=$v['typeproperty_id'];

			$attrData[$typeproperty_id][]=$v;

			
	
        }
		$nameData=array();
		foreach($attrData as $typeproperty_id=>$v){
			$name=M("typeproperty")->where("id='{$typeproperty_id}'")->getField('propertyname');
			$nameData[$typeproperty_id]['propertyname']=$name;
		}

        $this->assign('nameData',$nameData);
	
		
		$this->assign('attrData',$attrData);
		
     
     	$this->display();
     }
	 public function del(){
	 	$id=$_GET['id'];
		$this->model->where("id='{$id}'")->delete();
		$this->success("删除成功");
	
	 }

	
}
?>
	